package routers

import (
	"go_web_todo/controller"
	"go_web_todo/middleware"
	"go_web_todo/setting"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"

	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"

	"github.com/gin-gonic/gin"

	_ "go_web_todo/docs" // 这里需要引入本地已生成文档
)

func SetupRouter() *gin.Engine {
	if setting.Conf.Release {
		gin.SetMode(gin.ReleaseMode) //三种模式：debug、release、test
	}
	r := gin.Default()
	store := cookie.NewStore([]byte("something-very-secret"))
	// 访问 http://localhost:8888/swagger/index.html  下载 go get -u github.com/swaggo/swag/cmd/swag
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) //开启swagger，利用注释生成文档命令：swag init -g main.go
	r.Use(sessions.Sessions("mysession", store))
	r.Use(middleware.NewLogger(), middleware.Cors(), gin.Recovery()) //全局中间件注册，Recovery其实不用注册，Default路由中默认有
	v1 := r.Group("api/v1")
	{
		//用户操作
		v1.POST("user/register", controller.UserRegister)
		v1.POST("user/login", controller.Login)
		authed := v1.Group("/") //需要开启登录保护
		authed.Use(middleware.JWT())
		{
			// 任务操作
			authed.POST("task", controller.CreateTask)
			authed.GET("tasks", controller.GetTaskList)
			authed.GET("task/:id", controller.ShowTaskDetails)
			authed.DELETE("task/:id", controller.DeleteTaskById)
			authed.PUT("task/:id", controller.UpdateTask)
			authed.POST("search", controller.SearchTasks)
		}
	}
	return r
}
